package conexao;

import erro.ConexaoException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;

/**
 *
 * @author Roberta
 */
public class GerenciadorConexao {

    public String driver;
    private String url;
    private String user;
    private String pass;
    private static GerenciadorConexao instancia = null;
    private Connection conexao = null;

    private GerenciadorConexao() {

        ResourceBundle p = ResourceBundle.getBundle("conexao.banco");
        driver = p.getString("driver");
        url = p.getString("url");
        user = p.getString("user");
        pass = p.getString("pass");
    }

    public static GerenciadorConexao getInstancia() {
        if (instancia == null) {
            instancia = new GerenciadorConexao();
        }
        return instancia;
    }

    public Connection connect() throws ConexaoException {
        if (conexao == null) {
            try {
                Class.forName(driver);
                conexao = DriverManager.getConnection(url, user, pass);
            } catch (ClassNotFoundException ex) {
                throw new ConexaoException("Driver não encontrado");
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, ex);
                // throw new ConexaoException(ex);
            }
        }
        return conexao;
    }

    public void disconnect() throws ConexaoException {
        try {
            conexao.close();
            conexao = null;
        } catch (SQLException ex) {
            throw new ConexaoException(ex);
        }
    }
}
